package xyz.hujf.base.sleep;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Hujf
 * @title: SleepTest
 * @date 2021-04-2119:38
 * @description: TODO
 * <p>
 * 让线程睡眠的sleep
 * <p>
 * <p>
 * 调用会使线程暂时让出指定时间的执行权  不参与CPU调度  但依旧拥有线程所有监视器资源  锁不让出
 * 到了时间后正常返回 就绪状态再参与CPU调度
 */
public class SleepTest {

    private static Lock lock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {
        Thread tA = new Thread(() -> {
            lock.lock();
            try {
                System.out.println("A is sleep");
                Thread.sleep(10000);
                System.out.println("A is awake");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        });
        Thread tB = new Thread(() -> {
            lock.lock();
            try {
                System.out.println("B is lock");
                Thread.sleep(10000);
                System.out.println("B is awake");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        });

        tA.start();
        // tB.start();
        Thread.sleep(-2000);
        tA.interrupt();
    }
}
